home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / libpcap-0.0.6 / pcap-pf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-11  |  7.9 KB  |  311 lines

  1. /*
  2.  * Copyright (c) 1990, 1991, 1992, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21. #ifndef lint
  22. static  char rcsid[] =
  23.     "@(#)$Header: pcap-pf.c,v 1.32 94/06/10 17:41:01 mccanne Exp $ (LBL)";
  24. #endif
  25.  
  26. /*
  27.  * packet filter subroutines for tcpdump
  28.  *    Extraction/creation by Jeffrey Mogul, DECWRL
  29.  *
  30.  * Extracted from tcpdump.c.
  31.  */
  32.  
  33. #include <sys/types.h>
  34. #include <sys/time.h>
  35. #include <sys/timeb.h>
  36. #include <sys/socket.h>
  37. #include <sys/file.h>
  38. #include <sys/ioctl.h>
  39. #include <net/pfilt.h>
  40.  
  41. #include <net/if.h>
  42. #include <net/bpf.h>
  43.  
  44. #include <netinet/in.h>
  45. #include <netinet/in_systm.h>
  46. #include <netinet/ip.h>
  47. #include <netinet/if_ether.h>
  48. #include <netinet/ip_var.h>
  49. #include <netinet/udp.h>
  50. #include <netinet/udp_var.h>
  51. #include <netinet/tcp.h>
  52. #include <netinet/tcpip.h>
  53.  
  54. #include <ctype.h>
  55. #include <errno.h>
  56. #include <netdb.h>
  57. #include <signal.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <unistd.h>
  62.  
  63. #include "pcap-int.h"
  64.  
  65. /*
  66.  * BUFSPACE is the size in bytes of the packet read buffer.  Most tcpdump
  67.  * applications aren't going to need more than 200 bytes of packet header
  68.  * and the read shouldn't return more packets than packetfilter's internal
  69.  * queue limit (bounded at 256).
  70.  */
  71. #define BUFSPACE (200*256)
  72.  
  73. int
  74. pcap_read(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
  75. {
  76.     u_char *p;
  77.     struct bpf_insn *fcode;
  78.     int cc;
  79.     register u_char *bp;
  80.     int buflen, inc;
  81.     struct enstamp stamp;
  82.     int n;
  83.     fcode = pc->md.use_bpf ? 0 : pc->fcode.bf_insns;
  84.  again:
  85.     cc = pc->cc;
  86.     if (cc == 0) {
  87.         cc = read(pc->fd, (char *)pc->buffer, pc->bufsize);
  88.         if (cc < 0) {
  89.             if (errno == EWOULDBLOCK)
  90.                 return (0);
  91.             if (errno == EINVAL &&
  92.                 (long)(tell(pc->fd) + pc->bufsize) < 0) {
  93.                 /*
  94.                  * Due to a kernel bug, after 2^31 bytes,
  95.                  * the kernel file offset overflows and
  96.                  * read fails with EINVAL. The lseek()
  97.                  * to 0 will fix things.
  98.                  */
  99.                 (void)lseek(pc->fd, 0L, 0);
  100.                 goto again;
  101.             }
  102.             sprintf(pc->errbuf, "pf read: %s",
  103.                 pcap_strerror(errno));
  104.             return (-1);
  105.         }
  106.         bp = pc->buffer;
  107.     } else
  108.         bp = pc->bp;
  109.     /*
  110.      * Loop through each packet.
  111.      */
  112.     n = 0;
  113.     while (cc > 0) {
  114.         /* avoid alignment issues here */
  115.         bcopy((char *)bp, (char *)&stamp, sizeof(stamp));
  116.         if (stamp.ens_stamplen != sizeof(stamp))
  117.             /* buffer is garbage, treat it as poison */
  118.             break;
  119.  
  120.         p = bp + stamp.ens_stamplen;
  121.  
  122.         buflen = stamp.ens_count;
  123.         if (buflen > pc->snapshot)
  124.             buflen = pc->snapshot;
  125.  
  126.         /*
  127.          * Short-circuit evaluation: if using BPF filter
  128.          * in kernel, no need to do it now.
  129.          */
  130.         if (fcode == 0 ||
  131.             bpf_filter(fcode, p, stamp.ens_count, buflen)) {
  132.             struct pcap_pkthdr h;
  133.             pc->md.TotAccepted++;
  134.             h.ts = stamp.ens_tstamp;
  135.             h.len = stamp.ens_count;
  136.             h.caplen = buflen;
  137.             (*callback)(user, &h, p);
  138.             if (++n >= cnt && cnt > 0) {
  139.                 inc = ENALIGN(buflen + stamp.ens_stamplen);
  140.                 cc -= inc;
  141.                 bp += inc;
  142.                 pc->cc = cc;
  143.                 pc->bp = bp;
  144.                 return (n);
  145.             }
  146.         }
  147.         pc->md.TotPkts++;
  148.         pc->md.TotDrops += stamp.ens_dropped;
  149.         pc->md.TotMissed = stamp.ens_ifoverflows;
  150.         if (pc->md.OrigMissed < 0)
  151.             pc->md.OrigMissed = pc->md.TotMissed;
  152.  
  153.         inc = ENALIGN(buflen + stamp.ens_stamplen);
  154.         cc -= inc;
  155.         bp += inc;
  156.     }
  157.     pc->cc = 0;
  158.     return (n);
  159. }
  160.  
  161. int
  162. pcap_stats(pcap_t *p, struct pcap_stat *ps)
  163. {
  164.     ps->ps_recv = p->md.TotAccepted;
  165.     ps->ps_drop = p->md.TotDrops;
  166.     ps->ps_ifdrop = p->md.TotMissed - p->md.OrigMissed;
  167.     return (0);
  168. }
  169.  
  170. pcap_t *
  171. pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
  172. {
  173.     pcap_t *p;
  174.     short enmode;
  175.     int backlog = -1;    /* request the most */
  176.     struct enfilter Filter;
  177.     struct endevp devparams;
  178.  
  179.     p = (pcap_t *)malloc(sizeof(*p));
  180.     if (p == 0) {
  181.         strcpy(ebuf, "no swap");
  182.         return (0);
  183.     }
  184.     bzero(p, sizeof(*p));
  185.     p->fd = pfopen(device, 0);
  186.     if (p->fd < 0) {
  187.         sprintf(ebuf, "pf open: %s: %s\n\
  188. your system may not be properly configured; see \"man packetfilter(4)\"\n",
  189.             device, pcap_strerror(errno));
  190.         goto bad;
  191.     }
  192.     p->md.OrigMissed = -1;
  193.     enmode = ENTSTAMP|ENBATCH|ENNONEXCL;
  194.     if (promisc)
  195.         enmode |= ENPROMISC;
  196.     if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
  197.         sprintf(ebuf, "EIOCMBIS: %s", pcap_strerror(errno));
  198.         goto bad;
  199.     }
  200. #ifdef    ENCOPYALL
  201.     /* Try to set COPYALL mode so that we see packets to ourself */
  202.     enmode = ENCOPYALL;
  203.     (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
  204. #endif
  205.     /* set the backlog */
  206.     if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
  207.         sprintf(ebuf, "EIOCSETW: %s", pcap_strerror(errno));
  208.         goto bad;
  209.     }
  210.     /* set truncation */
  211.     if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&snaplen) < 0) {
  212.         sprintf(ebuf, "EIOCTRUNCATE: %s", pcap_strerror(errno));
  213.         goto bad;
  214.     }
  215.     p->snapshot = snaplen;
  216.     /* accept all packets */
  217.     Filter.enf_Priority = 37;    /* anything > 2 */
  218.     Filter.enf_FilterLen = 0;    /* means "always true" */
  219.     if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
  220.         sprintf(ebuf, "EIOCSETF: %s", pcap_strerror(errno));
  221.         goto bad;
  222.     }
  223.     /* discover interface type */
  224.     if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
  225.         sprintf(ebuf, "EIOCDEVP: %s", pcap_strerror(errno));
  226.         goto bad;
  227.     }
  228.     /* HACK: to compile prior to Ultrix 4.2 */
  229. #ifndef    ENDT_FDDI
  230. #define    ENDT_FDDI    4
  231. #endif
  232.     switch (devparams.end_dev_type) {
  233.     case ENDT_10MB:
  234.         p->linktype = DLT_EN10MB;
  235.         break;
  236.  
  237.     case ENDT_FDDI:
  238.         p->linktype = DLT_FDDI;
  239.         break;
  240.  
  241.     default:
  242.         /*
  243.          * XXX
  244.          * Currently, the Ultrix packet filter supports only
  245.          * Ethernet and FDDI.  Eventually, support for SLIP and PPP
  246.          * (and possibly others: T1?) should be added.
  247.          */
  248. #ifdef notdef
  249.         warning(
  250.            "Packet filter data-link type %d unknown, assuming Ethernet",
  251.             devparams.end_dev_type);
  252. #endif
  253.         p->linktype = DLT_EN10MB;
  254.         break;
  255.     }
  256.  
  257.     if (to_ms != 0) {
  258.         struct timeval timeout;
  259.         timeout.tv_sec = to_ms / 1000;
  260.         timeout.tv_usec = (to_ms * 1000) % 1000000;
  261.         if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
  262.             sprintf(ebuf, "EIOCSRTIMEOUT: %s",
  263.                 pcap_strerror(errno));
  264.             goto bad;
  265.         }
  266.     }
  267.     p->bufsize = BUFSPACE;
  268.     p->buffer = (u_char*)malloc(p->bufsize);
  269.  
  270.     return (p);
  271.  bad:
  272.     free(p);
  273.     return (0);
  274. }
  275.  
  276. int
  277. pcap_setfilter(pcap_t *p, struct bpf_program *fp)
  278. {
  279.     /*
  280.      * See if BIOCSETF works.  If it does, the kernel supports
  281.      * BPF-style filters, and we do not need to do post-filtering.
  282.      */
  283.     p->md.use_bpf = (ioctl(p->fd, BIOCSETF, (caddr_t)fp) >= 0);
  284.     if (p->md.use_bpf) {
  285.         struct bpf_version bv;
  286.  
  287.         if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) < 0) {
  288.             sprintf(p->errbuf, "BIOCVERSION: %s",
  289.                 pcap_strerror(errno));
  290.             return (-1);
  291.         }
  292.         else if (bv.bv_major != BPF_MAJOR_VERSION ||
  293.              bv.bv_minor < BPF_MINOR_VERSION) {
  294.             fprintf(stderr,
  295.         "requires bpf language %d.%d or higher; kernel is %d.%d",
  296.                 BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
  297.                   bv.bv_major, bv.bv_minor);
  298.             /* don't give up, just be inefficient */
  299.             p->md.use_bpf = 0;
  300.         }
  301.     } else
  302.         p->fcode = *fp;
  303.  
  304.     /*XXX this goes in tcpdump*/
  305.     if (p->md.use_bpf)
  306.         fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
  307.     else
  308.         fprintf(stderr, "tcpdump: Filtering in user process\n");
  309.     return (0);
  310. }
  311.